package com.doudou.dubbo.demo;

import com.doudou.dubbo.demo.api.GrettingServiceAsync;
import java.util.concurrent.CompletableFuture;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.RpcContext;

/**
 * @author zhaozw
 * @version 1.0
 * @date 2025-07-22 20:21
 **/
public class ApiConsumerForProviderAsync {

    public static void main(String[] args) throws InterruptedException {
        ReferenceConfig<GrettingServiceAsync> referenceConfig = new ReferenceConfig<>();
        referenceConfig.setApplication(new ApplicationConfig("first-dubbo-consumer"));
        referenceConfig.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
        referenceConfig.setInterface(GrettingServiceAsync.class);
        referenceConfig.setTimeout(5000);
        referenceConfig.setVersion("1.0.0");
        referenceConfig.setGroup("dubbo");

        GrettingServiceAsync grettingServiceAsync = referenceConfig.get();
        RpcContext.getContext()
                .setAttachment("company", "juzi");

        CompletableFuture<String> future = grettingServiceAsync.sayHello("world");
        future.whenComplete((v, t) -> {
            if (t != null) {
                t.printStackTrace();
            } else {
                System.out.println("----------------------->>> " + v);
            }
        });
        Thread.currentThread().join();
    }
}
